package ${basePackage}.biz.system.impl;

import ${basePackage}.biz.system.SystemJobBiz;
import ${basePackage}.core.constants.ResponseStatus;
import ${basePackage}.core.exception.BusinessException;
import ${basePackage}.dao.system.model.SystemJob;
import ${basePackage}.service.system.SystemJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemJobBizImpl implements SystemJobBiz {

    @Autowired
    private SystemJobService systemJobService;

    @Override
    public Integer create(SystemJob job) {
        job.setDisabled(Boolean.FALSE);
        SystemJob queryDto = new SystemJob();
        queryDto.setDisabled(Boolean.FALSE);
        queryDto.setDeleted(Boolean.FALSE);
        queryDto.setHandler(job.getHandler());
        if (systemJobService.count(queryDto) > 0) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "已存在任务处理器'" + job.getHandler() + "'，请勿重复绑定");
        }
        return systemJobService.create(job);
    }

    @Override
    public void updateById(SystemJob job) {
        SystemJob queryDto = new SystemJob();
        queryDto.setDisabled(Boolean.FALSE);
        queryDto.setDeleted(Boolean.FALSE);
        queryDto.setHandler(job.getHandler());
        List<SystemJob> jobs = systemJobService.findList(queryDto);
        if (jobs.size() > 1) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "已存在任务处理器'" + job.getHandler() + "'，请勿重复绑定");
        }
        if (jobs.size() == 1 && !jobs.get(0).getId().equals(job.getId())) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "已存在任务处理器'" + job.getHandler() + "'，请勿重复绑定");
        }
        systemJobService.updateById(job);
    }
}
